﻿using System;
using System.Collections.Generic;
using System.Linq;
using FoodRaidersWebAPI.domainnew.Article;
using FoodRaidersWebAPI.domainnew.Comment;
using FoodRaidersWebAPI.domainnew.other;
using FoodRaidersWebAPI.Models;
using FoodRaidersWebAPI.Service.Second;
using Microsoft.EntityFrameworkCore;

namespace FoodRaidersWebAPI.Service.First
{
    public class FoodArticleService
    {
        private readonly FoodContext context;
        private readonly FoodContext foodContext;
        private readonly ArticleService articleService;
        private readonly FoodService foodService;

        public FoodArticleService(FoodContext context)
        {
            foodContext=this.context = context;
            articleService = new ArticleService(context);
            foodService = new FoodService(context);
        }

        /**
         * author:陈奇廷
         */
        //id查询 对象不加载
        public FoodArticle findByID(int id)
        {
            return context.FoodArticles.FirstOrDefault(f => f.FoodArticleID == id);
        }
        //id的收藏数+1
        public void addFavorite(int id)
        {
            var foodArticle = context.FoodArticles.FirstOrDefault(f => f.FoodArticleID == id);
            articleService.addFavorite(foodArticle.ArticleID);
        }
        //id的收藏数-1
        public void subFavorite(int id)
        {
            var foodArticle = context.FoodArticles.FirstOrDefault(f => f.FoodArticleID == id);
            articleService.subFavorite(foodArticle.ArticleID);
        }
        //id 查询 不加载评论（用户）
        public FoodArticle findByIDNoCommentLoad(int id)
        {
            var foodArticle = findByID(id);
            foodArticle.Article = articleService.findByIDNoCommentLoad(foodArticle.ArticleID);
            foodArticle.Food = foodService.findByIDFullLoad(foodArticle.FoodID);
            return foodArticle;
        }



        /**
         * author:曹昕凯
         */
        public List<FoodArticle> ListByPraise()//按点赞数从大到小（全部）
        {
            var query = foodContext.FoodArticles.OrderByDescending(a => a.Article.PraiseNumber).ToList();
            if(query!=null)
            {
                for(int i=0;i<query.Count;i++)
                {
                    foodContext.Entry(query[i]).Reference(a => a.Article).Load();
                    foodContext.Entry(query[i]).Reference(a => a.Food).Load();
                    foodContext.Entry(query[i].Article).Collection(a => a.Pictures).Load();
                    foodContext.Entry(query[i].Food).Collection(a => a.LabelObjects).Load();
                }
            }
            return query;
        }

        public List<FoodArticle> ListByPraise(int n)//按点赞数从大到小（前n个）
        {
            var query = foodContext.FoodArticles.OrderByDescending(a => a.Article.PraiseNumber).Take(n).ToList();
            if (query != null)
            {
                for (int i = 0; i < query.Count; i++)
                {
                    foodContext.Entry(query[i]).Reference(a => a.Article).Load();
                    foodContext.Entry(query[i]).Reference(a => a.Food).Load();
                    foodContext.Entry(query[i].Article).Collection(a => a.Pictures).Load();
                    foodContext.Entry(query[i].Food).Collection(a => a.LabelObjects).Load();
                }
            }
            return query;
        }

        public List<FoodArticle> ListByDateTime()//按发布时间从大到小（全部）
        {
            var query = foodContext.FoodArticles.OrderByDescending(a => a.Article.DateTime).ToList();
            if (query != null)
            {
                for (int i = 0; i < query.Count; i++)
                {
                    foodContext.Entry(query[i]).Reference(a => a.Article).Load();
                    foodContext.Entry(query[i]).Reference(a => a.Food).Load();
                    foodContext.Entry(query[i].Article).Collection(a => a.Pictures).Load();
                    foodContext.Entry(query[i].Food).Collection(a => a.LabelObjects).Load();
                }
            }
            return query;
        }

        public List<FoodArticle> ListByDateTime(int n)//按发布时间从大到小（前n个）
        {
            var query = foodContext.FoodArticles.OrderByDescending(a => a.Article.DateTime).Take(n).ToList();
            if (query != null)
            {
                for (int i = 0; i < query.Count; i++)
                {
                    foodContext.Entry(query[i]).Reference(a => a.Article).Load();
                    foodContext.Entry(query[i]).Reference(a => a.Food).Load();
                    foodContext.Entry(query[i].Article).Collection(a => a.Pictures).Load();
                    foodContext.Entry(query[i].Food).Collection(a => a.LabelObjects).Load();
                }
            }
            return query;
        }

        public FoodArticle Find(int id)//查找特定攻略
        {
            var foodArticle = foodContext.FoodArticles.FirstOrDefault(a => a.FoodArticleID == id);
            if(foodArticle!=null)
            {
                foodContext.Entry(foodArticle).Reference(a => a.Article).Load();
                foodContext.Entry(foodArticle).Reference(a => a.Food).Load();
                foodContext.Entry(foodArticle.Article).Collection(a => a.Pictures).Load();
                foodContext.Entry(foodArticle.Food).Collection(a => a.LabelObjects).Load();
                foodContext.Entry(foodArticle.Article).Collection(a => a.ArticleComments).Load();
                foodArticle.Article.ArticleComments.ForEach(a =>
                {
                    context.Entry(a).Reference(b=>b.User).Load();
                });
            }
            return foodArticle;
        }

        public void AddPraise(int id)//点赞
        {
            var foodArticle = foodContext.FoodArticles.FirstOrDefault(a => a.FoodArticleID == id);
            if (foodArticle == null)
                return;
            foodContext.Entry(foodArticle).Reference(a => a.Article).Load();
            Article article = foodArticle.Article;
            foodContext.Entry(article).State = EntityState.Modified;
            foodArticle.Article.PraiseNumber++;
            foodContext.SaveChanges();
        }

        public void SubPraise(int id)//取消点赞
        {
            var foodArticle = foodContext.FoodArticles.FirstOrDefault(a => a.FoodArticleID == id);
            if (foodArticle == null)
                return;
            foodContext.Entry(foodArticle).Reference(a => a.Article).Load();
            foodContext.Entry(foodArticle.Article).State = EntityState.Modified;
            foodArticle.Article.PraiseNumber--;
            foodContext.SaveChanges();
        }

        public User GetWriter(int id)//获取当前文章作者头像和用户名
        {
            var foodArticle = foodContext.FoodArticles.FirstOrDefault(a => a.FoodArticleID == id);
            if (foodArticle == null)
                return null;
            var user = foodContext.Users.FirstOrDefault(a => a.UserID == foodArticle.UserID);
            if (user == null)
                return null;
            User writer = new User();
            writer.Name = user.Name;
            writer.AvatarUrl = user.AvatarUrl;
            return writer;
        }

        public void AddComment(int id,string content,int uid)
        {
            var FoodArticle = foodContext.FoodArticles.FirstOrDefault(a => a.FoodArticleID == id);
            if (FoodArticle != null)
            {
                ArticleComment articleComment = new ArticleComment();
                articleComment.Content = content;
                articleComment.UserID = uid;
                articleComment.PraiseNumber = 0;
                articleComment.DateTime = DateTime.Now;
                foodContext.Entry(FoodArticle).Reference(a => a.Article).Load();
                foodContext.Entry(FoodArticle.Article).Collection(a => a.ArticleComments).Load();
                FoodArticle.Article.ArticleComments.Add(articleComment);
                foodContext.SaveChanges();
            }
            else throw new ArgumentNullException();
        }
        
        public List<FoodArticle> ListByLabel(Label label)
        {
            var query = foodContext.FoodArticles.Where(a => a.Food.LabelObjects.Any(b => b.Label == label)).OrderByDescending(a => a.Article.PraiseNumber).ToList();
            if (query != null)
            {
                for (int i = 0; i < query.Count; i++)
                {
                    foodContext.Entry(query[i]).Reference(a => a.Article).Load();
                    foodContext.Entry(query[i]).Reference(a => a.Food).Load();
                    foodContext.Entry(query[i].Article).Collection(a => a.Pictures).Load();
                    foodContext.Entry(query[i].Food).Collection(a => a.LabelObjects).Load();
                }
            }
            return query;
        }

        public void AddFoodArticle(FoodArticle foodArticle)
        {
            foodContext.FoodArticles.Add(foodArticle);
            foodContext.SaveChanges();
        }
        
        public FoodArticle Delete(int id)
        {
            FoodArticle foodArticle = Find(id);
            context.FoodArticles.Remove(foodArticle);
            context.SaveChanges();
            foodArticle.Article=articleService.delete(foodArticle.ArticleID);
            foodArticle.Food=foodService.delete(foodArticle.FoodID);
            return foodArticle;
        }
        
        public List<FoodArticle> Location()
        {
            var query = foodContext.FoodArticles.ToList();
            if (query != null)
            {
                for (int i = 0; i < query.Count; i++)
                {
                    foodContext.Entry(query[i]).Reference(a => a.Food).Load();
                }
            }
            return query;
        }

    }
}